﻿@using Sovigaz
@{
    ViewBag.Title = "Chi tiết vai trò";
    var root = MvcApplication.SiteMaps.Where(p => String.IsNullOrWhiteSpace(p.ParentID)).OrderBy(p => p.Order).ToList();
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('RoleController', function ($scope, $compile, $http, service) {
        $scope.load = function () {
            startloading($('#content'));
            var url = '@url' + '@Url.Action("RoleApi", "Api", new { id = Model })';

            $http.get(url)
                .success(function (data) {
                    for (var i = 0; i < data.RolePermissions.length; i++) {
                        $scope.rp[data.RolePermissions[i].SitemapID + '_' + data.RolePermissions[i].PermissionID] = data.RolePermissions[i].IsActived;
                    }

                    $scope.role = data;
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.refresh = function () {
            $scope.load();
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "Role", new { id = String.Empty })';
        };

        $scope.save = function () {
            $scope.role.RolePermissions = [];
            for (var s in $scope.rp) {
                if (s !== '0') {
                    var tmp = s.split('_');
                    $scope.role.RolePermissions.push({ "RoleID": $scope.role.RoleID, "SitemapID": tmp[0], "PermissionID": tmp[1], "IsActived": $scope.rp[s] });
                }
            }

            var url = '@url' + '@Url.Action("RoleApi", "Api")';
            $http.put(url, $scope.role)
                .success(function (data) {
                    if (@Model == 0) {
                        window.location = '@Url.Action("Detail", "Role")/' + data.RoleID;
                    }
                    $scope.role = data;

                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được lưu thành công!!!');
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var url = '@url' + '@Url.Action("RoleApi", "Api")';
            $http.delete(url, {
                params: {
                    id: $scope.role.RoleID
                }
            }).success(function (data) {
                stoploading($('#content'));
                window.location = '@Url.Action("Detail", "Role", new { id = String.Empty })';
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.back = function () {
            window.location = '@Url.Action("Index", "Role")';
        };

        $scope.collapse = function(sitemapID) {
            if ($scope.state[sitemapID] == undefined)
                $scope.state[sitemapID] = false;
            else
                $scope.state[sitemapID] = !$scope.state[sitemapID];
        };

        $scope.parent_change = function (sitemapID, permission) {
            var url = '@url' + '@Url.Action("SitemapApi", "Api")/' + sitemapID;

            $http.get(url)
                .success(function (data) {
                    for (var i = 0; i < data.length; i++) {
                        $scope.rp[data[i].SitemapID + '_' + permission] = $scope.rp[sitemapID + '_' + permission];
                    }
                })
                .error(function (data) {
                    service.showModal($scope, $compile, 'error', data);
                });
        };

        $scope.child_change = function (parentID, sitemapID, permission) {
            if (!$scope.rp[sitemapID + '_' + permission])
                $scope.rp[parentID + '_' + permission] = false;
        };

        $scope.rp = [{}];
        $scope.state = [{}];
        $scope.load();
    });

    $(function () {
        $("[data-toggle=tooltip]").tooltip();
    });
</script>
<div ng-controller="RoleController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_Toolbar")
    <div id="content" class="content row">
        <form role="form" name="form">
            <div class="panel-body">
                <div class="col-lg-4">
                    @Html.Partial("Control/_Control_TextBox_Required", new Dictionary<string, string>
                    {
                        {"Text", "Vai trò"},
                        {"Model", "role.RoleName"},
                        {"Name", "roleName"},
                        {"Message", "Tên vai trò bắt buộc nhập."}
                    })
                </div>
                <div class="col-lg-8" style="clear: both">
                    @Html.Partial("Control/_Control_TextArea", new Dictionary<string, string>
                    {
                        {"Text", "Ghi chú"},
                        {"Model", "role.Description"},
                        {"Name", "description"}
                    })
                    @Html.Partial("Control/_Control_CheckBox", new Dictionary<string, string>
                    {
                        {"Text", "Đang sử dụng"},
                        {"Model", "role.IsActived"}
                    })
                </div>
                <div class="col-lg-8" style="clear: both; border-top: 1px solid lightgray; margin-top: 10px; margin-bottom: 10px">

                </div>
                <div class="col-lg-8" style="clear: both">
                    <!-- /.panel-heading -->
                    <div class="panel-in-detail panel-body">
                        <!-- Nav tabs -->
                        <ul class="nav nav-tabs">
                            <li class="active">
                                <a href="#permission" data-toggle="tab">Danh sách màn hình</a>
                            </li>
                            <li>
                                <a href="#user" data-toggle="tab">Danh sách người sử dụng</a>
                            </li>
                        </ul>
                        <!-- Tab panes -->
                        <div class="tab-content">
                            <div class="tab-pane fade in active" id="permission">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered table-hover">
                                        <colgroup>
                                            <col />
                                            <col style="width: 100px" />
                                            <col style="width: 100px" />
                                            <col style="width: 100px" />
                                        </colgroup>
                                        <thead>
                                            <tr>
                                                <th>
                                                    Màn hình
                                                </th>
                                                <th class="center">
                                                    Xem
                                                </th>
                                                <th class="center">
                                                    Thêm/Sửa
                                                </th>
                                                <th class="center">
                                                    Xóa
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach (var r in root)
                                            {
                                                var child1 = MvcApplication.SiteMaps.Where(p => p.ParentID == r.SitemapID).OrderBy(p => p.Order).ToList();
                                                <tr>
                                                    <td>
                                                        <i ng-click="collapse('@r.SitemapID')" ng-class="state['@r.SitemapID'] == true || state['@r.SitemapID'] == null ? 'fa-minus-circle' : 'fa-plus-circle'"
                                                           class="fa fa-minus-circle fa-fw"></i><span>@r.Title</span>
                                                    </td>
                                                    <td class="center">
                                                        @if (r.Type.Contains("1"))
                                                        {
                                                            <input ng-click="parent_change('@r.SitemapID', '1')"
                                                                   ng-model="rp['@r.SitemapID' + '_1']" type="checkbox" />
                                                        }
                                                    </td>
                                                    <td class="center">
                                                        @if (r.Type.Contains("2"))
                                                        {
                                                            <input ng-click="parent_change('@r.SitemapID', '2')"
                                                                   ng-model="rp['@r.SitemapID' + '_2']" type="checkbox" />
                                                        }
                                                    </td>
                                                    <td class="center">
                                                        @if (r.Type.Contains("3"))
                                                        {
                                                            <input ng-click="parent_change('@r.SitemapID', '3')"
                                                                   ng-model="rp['@r.SitemapID' + '_3']" type="checkbox" />
                                                        }
                                                    </td>
                                                </tr>
                                                foreach (var c1 in child1)
                                                {
                                                    <tr ng-show="state['@r.SitemapID'] == true || state['@r.SitemapID'] == null">
                                                        <td><span style="margin-left: 40px">@c1.Title</span></td>
                                                        <td class="center">
                                                            @if (c1.Type.Contains("1"))
                                                            {
                                                                <input ng-click="child_change('@r.SitemapID', '@c1.SitemapID', '1')"
                                                                       ng-model="rp['@c1.SitemapID' + '_1']" type="checkbox" />
                                                            }
                                                        </td>
                                                        <td class="center">
                                                            @if (c1.Type.Contains("2"))
                                                            {
                                                                <input ng-click="child_change('@r.SitemapID', '@c1.SitemapID', '2')"
                                                                       ng-model="rp['@c1.SitemapID' + '_2']" type="checkbox" />
                                                            }
                                                        </td>
                                                        <td class="center">
                                                            @if (c1.Type.Contains("3"))
                                                            {
                                                                <input ng-click="child_change('@r.SitemapID', '@c1.SitemapID', '3')"
                                                                       ng-model="rp['@c1.SitemapID' + '_3']" type="checkbox" />
                                                            }
                                                        </td>
                                                    </tr>
                                                }
                                            }
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div class="tab-pane fade" id="user">
                                <div class="table-responsive">
                                    <table class="table table-striped table-bordered table-hover">
                                        <colgroup>
                                            <col />
                                            <col style="width: 150px" />
                                        </colgroup>
                                        <thead>
                                            <tr>
                                                <th>
                                                    Tên người sử dụng
                                                </th>
                                                <th>
                                                    Tên đăng nhập
                                                </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr ng-hide="role.UserRoleComplexs.length == 0" ng-repeat="ur in role.UserRoleComplexs">
                                                <td>{{ur.FullName}}</td>
                                                <td>{{ur.Username}}</td>
                                            </tr>
                                            <tr ng-show="role.UserRoleComplexs.length == 0">
                                                <td colspan="100">
                                                    Không có dữ liệu
                                                </td>
                                            </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>